package com.stalltea.jdk8.lambda.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author yivlchangfa
 * @since 2021/9/13 18:27
 */
public class SteamStudy {

    public static void main(String[] args) {

        Stream<int[]> s = IntStream.rangeClosed(1, 100).boxed()
                .flatMap(i -> IntStream.rangeClosed(i, 100)
                        .filter(j -> Math.sqrt(i * i + j * j) % 1 == 0)
                        .mapToObj(j -> new int[] { i, j, (int) Math.sqrt(i * i + j * j) })
                );
        List<int[]> list = s.collect(Collectors.toList());
        list.size();
    }

    private static void intStreamRange() {
        long rangeClosedCount = IntStream.rangeClosed(1, 100).filter(i -> i % 2 == 0).count();
        long rangeCount = IntStream.range(1, 100).filter(i -> i % 2 == 0).count();
        System.out.println(rangeClosedCount + "  " + rangeCount);
    }

    private static void unboxed() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        int sum = list.stream().mapToInt(e -> e).sum();
        System.out.println(sum);
    }

    public static void flatMapStudy() {
        List<Integer> array1 = Arrays.asList(1, 2, 3);
        List<Integer> array2 = Arrays.asList(4, 5);
        List<int[]> result = array1.stream().flatMap(i -> array2.stream().map(j -> new int[] { i, j })).collect(Collectors.toList());
        System.out.println(result);
    }

    public static void anyMatchStudy() {
        List<String> list = Arrays.asList("jack", "lucy", "linda");
        System.out.println(list.stream().anyMatch("lucy"::equals));
    }

}
